
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;


public class Offer32 {
    public int[] levelOrder(TreeNode root) {
        if (root==null){
            return new int[0];
        }
        Deque<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        ArrayList<Integer> result = new ArrayList<>();
        while (!queue.isEmpty()){
            TreeNode temp = queue.removeFirst();
            if (temp.left!=null){
                queue.add(temp.left);
            }
            if (temp.right!=null){
                queue.add(temp.right);
            }
            result.add(temp.val);
        }
        int[] re = new int[result.size()];
        for (int i = 0; i < re.length; i++) {
            re[i]=result.get(i);
        }
        return re;
    }
}
